package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicExpensesDeviceDTO;
import com.iwomy.secureplat.platform.entity.BasicExpensesDeptDetailEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicConsumptionRecordService;
import com.iwomy.secureplat.platform.service.BasicExpensesDeptDetailService;
import com.iwomy.secureplat.platform.service.BasicInventoryListService;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import com.iwomy.secureplat.platform.vo.QuerySmsTtsCountVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 单位资费配置详情
 *
 * @author pig
 * @date 2023-12-04 13:54:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicExpensesDeptDetail")
@Tag(description = "basicExpensesDeptDetail", name = "单位资费配置详情管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicExpensesDeptDetailController {

	private final BasicExpensesDeptDetailService basicExpensesDeptDetailService;

	private final RemoteDeptUtils remoteDeptUtils;

	private final BasicInventoryListService inventoryListService;

	private final BasicConsumptionRecordService consumptionRecordService;

	/**
	 * 分页查询
	 *
	 * @param page    分页对象
	 * @param expensesDeviceDTO 单位资费配置详情
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_view')")
	public R getBasicExpensesDeptDetailPage(@ParameterObject Page page, BasicExpensesDeviceDTO expensesDeviceDTO) {
		return R.ok(basicExpensesDeptDetailService.getBasicExpensesDeptDetailPage(page, expensesDeviceDTO));
	}


	/**
	 * 通过id查询单位资费配置详情
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicExpensesDeptDetailService.getById(id));
	}

	/**
	 * 通过单位id查询单位资费配置详情
	 *
	 * @param deptId
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/byDeptId/{deptId}")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_view')")
	public R byDeptId(@PathVariable("deptId") Long deptId) {
		return R.ok(basicExpensesDeptDetailService.byDeptId(deptId));
	}

	/**
	 * 通过单位id查询代付详情
	 *
	 * @param deptId
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/payingDetails/{deptId}")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_view')")
	public R payingDetails(@PathVariable("deptId") Long deptId) {
		return R.ok(basicExpensesDeptDetailService.payingDetails(deptId));
	}

	/**
	 * 通过单位id查询单位资费数量
	 *
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/querySmsTtsCount")
	public R querySmsTtsCount() {
		QuerySmsTtsCountVO querySmsTtsCountVO = new QuerySmsTtsCountVO();
		querySmsTtsCountVO = basicExpensesDeptDetailService.querySmsTtsCount();
		Integer thisMonthDeviceTotal = inventoryListService.tenantDeptDeviceNum(SecurityUtils.getUser().getDeptId());
		Integer deviceRemainNum = basicExpensesDeptDetailService.deviceRemainNum();
		Map<String, Integer> tenantDeptDeviceNum = consumptionRecordService.tenantQuerySmsTtsCount(SecurityUtils.getUser().getDeptId());
		querySmsTtsCountVO.setQueryLastMonthSmsAddTotal(tenantDeptDeviceNum.get("thisMonthSmsTotal"));
		querySmsTtsCountVO.setQueryLastMonthTtsAddTotal(tenantDeptDeviceNum.get("thisMonthTtsTotal"));
		querySmsTtsCountVO.setQueryLastMonthDeviceAddTotal(thisMonthDeviceTotal + deviceRemainNum);

		return R.ok(querySmsTtsCountVO);
	}


	/**
	 * 新增单位资费配置详情
	 *
	 * @param basicExpensesDeptDetail 单位资费配置详情
	 * @return R
	 */
	@Operation(summary = "新增单位资费配置详情", description = "新增单位资费配置详情")
	@SysLog("新增单位资费配置详情")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_add')")
	public R save(@RequestBody BasicExpensesDeptDetailEntity basicExpensesDeptDetail) {
		if (basicExpensesDeptDetail.getDeptId().equals(SecurityUtils.getUser().getDeptId())) {
//			throw new PlatformException("无法给本企业进行配置");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.EXPENSESDEPT_ADDED));
		}
		return basicExpensesDeptDetailService.newlyAdded(basicExpensesDeptDetail);
	}

	/**
	 * 代付单位资费配置详情
	 *
	 * @param basicExpensesDeptDetail 代付单位资费配置详情
	 * @return R
	 */
	@Operation(summary = "代付单位资费配置详情", description = "代付资费配置详情")
	@SysLog("代付单位资费配置详情")
	@PostMapping("/paying")
	public R paying(@RequestBody BasicExpensesDeptDetailEntity basicExpensesDeptDetail) {
		if (basicExpensesDeptDetail.getDeptId().equals(SecurityUtils.getUser().getDeptId())) {
			throw new PlatformException("无法给本企业进行配置");
		}
		System.out.println("basicExpensesDeptDetail.getSmsExpirationDate()==========" + basicExpensesDeptDetail.getTtsExpirationDate());
		return basicExpensesDeptDetailService.paying(basicExpensesDeptDetail);
	}

	/**
	 * 修改单位资费配置详情
	 *
	 * @param basicExpensesDeptDetail 单位资费配置详情
	 * @return R
	 */
	@Operation(summary = "修改单位资费配置详情", description = "修改单位资费配置详情")
	@SysLog("修改单位资费配置详情")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_edit')")
	public R updateById(@RequestBody BasicExpensesDeptDetailEntity basicExpensesDeptDetail) {
		return R.ok(basicExpensesDeptDetailService.updateById(basicExpensesDeptDetail));
	}

	/**
	 * 通过id删除单位资费配置详情
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除单位资费配置详情", description = "通过id删除单位资费配置详情")
	@SysLog("通过id删除单位资费配置详情")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicExpensesDeptDetailService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicExpensesDeptDetail 查询条件
	 * @param ids                     导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDeptDetail_export')")
	public List<BasicExpensesDeptDetailEntity> export(BasicExpensesDeptDetailEntity basicExpensesDeptDetail, Long[] ids) {
		return basicExpensesDeptDetailService.list(Wrappers.lambdaQuery(basicExpensesDeptDetail).in(ArrayUtil.isNotEmpty(ids), BasicExpensesDeptDetailEntity::getId, ids));
	}

}